package com.goldplusgold.tradedelay.app.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.tradedelay.app.base.constant.IntentConstant;
import com.goldplusgold.tradedelay.app.my.fragment.LoginFragment;
import com.goldplusgold.tradedelay.app.my.fragment.PersonalInfoFragment;
import com.goldplusgold.tradedelay.app.base.utils.ActivityUtils;
import com.goldplusgold.support.lib.utils.FragmentManagerUtil;

/**
 * Created by admin on 2017/5/26.
 */

public class UserActivity extends BaseActivity {
    private Intent intent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intent = getIntent();
        if (intent != null && intent.hasExtra("fromPage") && intent.getStringExtra("fromPage").equals("PersonalInfo")) {
            FragmentManagerUtil.getInstance().openFragment(this, PersonalInfoFragment.newInstance(getIntent().getStringExtra("headImage"), getIntent().getStringExtra("userName"),
                    getIntent().getStringExtra("wechatName"), getIntent().getBooleanExtra("wechatState", false)), true);
        } else {
            switchFragment();
        }

    }

    private void switchFragment() {
        FragmentManagerUtil.getInstance().openFragment(this, LoginFragment.newInstance(), true);
    }

    public void operationAfterLogin() {
        if (intent != null && intent.hasExtra(IntentConstant._TYPE)) {
            if (intent.getStringExtra(IntentConstant._TYPE).equals("forceQuit")) {
                ActivityUtils.isShowForcequitDialog = false;
            }
            if (intent.getStringExtra(IntentConstant._TYPE).equals("forceQuit") || intent.getStringExtra(IntentConstant._TYPE).equals("h5")) {
                ActivityUtils.skipToTDMain(UserActivity.this, "forceQuit");
            }
        } else {
            UserActivity.this.finish();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (intent != null && intent.hasExtra(IntentConstant._TYPE)) {
                switch (intent.getStringExtra(IntentConstant._TYPE)) {
                    case "forceQuit":
                        ActivityUtils.isShowForcequitDialog = false;
                        ActivityUtils.skipToTDMain(UserActivity.this, "forceQuit");
                        break;
                    case "h5":
                        ActivityUtils.skipToTDMain(UserActivity.this, "forceQuit");
                        break;
                }

            }
        }
        return super.onKeyUp(keyCode, event);
    }

}
